package LeetCode.month02.day57;

public class No200 {
    int[][] dir = {{0,1},{0,-1},{1,0},{-1,0}};
    boolean[][] visited;
    int res = 0;
    public int numIslands(char[][] grid) {
        visited = new boolean[grid.length][grid[0].length];
        int m = grid.length;
        int n = grid[0].length;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if(!visited[i][j]&&grid[i][j]!='0'){
                    dfs(grid,i,j);
                    res++;
                }
            }
        }
        return res;
    }

    public void dfs(char[][] grid,int m,int n){
        visited[m][n]=true;
        for (int i = 0; i < 4; i++) {
            int row = m + dir[i][0];
            int col = n + dir[i][1];
            if(row>=0&&row<grid.length&&col<grid[0].length&&col>=0&&grid[row][col]=='1'&&!visited[row][col]){
                dfs(grid,row,col);
            }
        }
    }
}
